package org.filecollector.msgcontroller.answers;

import com.google.gson.Gson;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.filecollector.msgcontroller.answers.responsecodes.CodeConvertDictionary;
import org.filecollector.msgcontroller.answers.responsecodes.ResponseCode;

public class SerializationTest {
    static Gson gson = new Gson();

    AnswerMessage getMSG() {
        return new AnswerMessage(
                ResponseCode.OK,
                "https://smt.smt/a?as=as#agjg%15%ff%a1!$&:[]@'()+*,;=",
                "EmptyData"
        );
    }

    String getSTR(AnswerMessage msg) {
        return String.format(
                "{\"code\":%d,\"uri\":%s,\"data\":\"%s\"}",
                CodeConvertDictionary.get(msg.code()),
                gson.toJson(msg.seeOtherURI().toString()),
                msg.data()
        );
    }

    @Before
    @Test
    public void testSerializationAM() {
        var msg = getMSG();
        var expected = getSTR(msg);
        TestCase.assertEquals(expected, msg.serialize());
    }

    @Test
    public void testSerializationSAM() {
        var msg = new SyncAnswerMessage(100L, getMSG());
        var expected = String.format(
                "{\"operation_id\":%d,\"message\":%s}",
                msg.operationID(),
                msg.answerMessage().serialize()
        );
        TestCase.assertEquals(expected, msg.serialize());
    }

    @Before
    @Test
    public void testDeserializationAM() {
        var expected = getMSG();
        var str = getSTR(expected);
        var actual = AnswerMessage.deserialize(getSTR(expected));
        TestCase.assertEquals(expected, actual);
    }

    @Test
    public void testDeserializationSAM() {
        var msg = new SyncAnswerMessage(100L, getMSG());
        var expected = String.format(
                "{\"operation_id\":%d,\"message\":%s}",
                msg.operationID(),
                msg.answerMessage().serialize()
        );
        var actual = SyncAnswerMessage.deserialize(expected);
        TestCase.assertEquals(msg, actual);
    }
}
